package co.yixiang.modules.utils;

import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName : DragonMap
 * @Description :
 * @Author : DragonUncle
 * @Date :  15:24 2020/5/13
 */
@Component
public class MapUtils {

    public static Map<String,Object> entityToMap(Object object){
        if (object == null) return new HashMap<>();
        Field[] declaredFields = object.getClass().getDeclaredFields();
        Map<String, Object> map = new HashMap<>();
        for (Field declaredField : declaredFields) {
            try {
                declaredField.setAccessible(true);
                map.put(declaredField.getName(),declaredField.get(object));
            } catch (IllegalAccessException e) {
                map.put(declaredField.getName(),null);
            }
        }
        return map;
    }
}
